//
//  BindingInfoViewController.m
//  CarCare
//
//  Created by ileo on 14-8-14.
//  Copyright (c) 2014年 baozun. All rights reserved.
//

#import "DeviceInfoViewController.h"
#import "NetCar.h"
#import "RefreshMgr.h"
#import "DeviceHeadCell.h"
#import "BaseWebViewController.h"

typedef NS_ENUM(NSInteger , Cell_Views_Type) {
    Cell_Views_Type_LeftLabel = 10,
    Cell_Views_Type_RightLabel = 20,
    Cell_Views_Type_CenterLabel = 30,
    
};

#define CELL_HEIGHT (45 * __MAIN_RATIO_375)
#define CELL_HEIGHT_CENTER (136.5 * __MAIN_RATIO_375)

@interface DeviceInfoViewController ()<UITableViewDelegate,UITableViewDataSource,UIViewControllerNavBackgroundDataSource>

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic ,strong) RefreshMgr *refreshMgr;

@property (nonatomic, readonly) NSArray *dataArr;

@property (nonatomic , copy) NSString * lastTimeStr;

//设备状态
@property (nonatomic , copy) NSString * deviceStatus;

@end

@implementation DeviceInfoViewController
- (void)dealloc
{

}
- (instancetype)initWithMainViewType:(TYPE_MAIN_VIEW)type withDeviceStatus:(NSString *)deviceStatus
{
    self = [super initWithMainViewType:type];
    if (self) {
        self.deviceStatus = deviceStatus;
    }
    return self;
}
- (NSArray *)dataArr
{
    return @[@[LOCSTRING(@"binding_dev_status")],@[LOCSTRING(@"binging_line_time")],@[@"空白"],@[@"用户ID",LOCSTRING(@"binding_dev"),LOCSTRING(@"binding_time"),LOCSTRING(@"binding_tel")],@[@"空白"]];
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, -64, __MAIN_WIDTH, __MAIN_HEIGHT) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.backgroundColor = [UIColor whiteColor];
    _tableView.separatorColor = [UIColor clearColor];
    [self.view addSubview:_tableView];
    


    self.refreshMgr = [RefreshMgr sharedInstance];
    [self.refreshMgr.refreshDeviceStatus startRefreshWithTimeInterval:0];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateDiviceStatus) name:REFRESH_DEVICESTATUS object:nil];
    
    _lastTimeStr = self.refreshMgr.refreshDeviceStatus.lastUseDate;

}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.refreshMgr.refreshDeviceStatus stopRefresh];
    
}
- (void)viewDidAppear:(BOOL)animated
{
    [self.refreshMgr.refreshDeviceStatus startRefreshWithTimeInterval:0];
}
- (void)updateDiviceStatus
{
    if([self.deviceStatus isNotNil] && (![self.deviceStatus isEqualToString:self.refreshMgr.refreshDeviceStatus.status]))
    {
        self.deviceStatus = self.refreshMgr.refreshDeviceStatus.status;
        [self.tableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0],[NSIndexPath indexPathForRow:0 inSection:1]] withRowAnimation:UITableViewRowAnimationNone];
    }
    _lastTimeStr = self.refreshMgr.refreshDeviceStatus.lastUseDate;

}
#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.dataArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	return [self.dataArr[section] count];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return (indexPath.section == 0 ? [DeviceHeadCell getCellHeight] : (indexPath.section == 1 ? CELL_HEIGHT_CENTER : (indexPath.section == 3 ? CELL_HEIGHT : SECTION_HEIGHT)));
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell;
    UIColor * textLabelColor = COLOR_HEXSTRING(@"#FFFFFF");
    CGFloat labelX = 30 * __MAIN_RATIO_375;
    switch (indexPath.section) {
        case 0:
        {
            NSString *identifier = @"CellDeviceStates";
            cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (cell == nil) {
                cell = [[DeviceHeadCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
                cell.backgroundColor = [UIColor clearColor];

            }
            cell.backgroundColor = ([self.deviceStatus isEqualToString:@"设备正常"] ? COLOR_HEXSTRING(@"#1f90e5") :  ([self.deviceStatus isEqualToString:@"设备异常"] ?COLOR_HEXSTRING(@"#ef5f33") : [UIColor whiteColor]));
            ((DeviceHeadCell *)cell).deviceStatusStr = self.deviceStatus;
            __weak __typeof(self) wself = self;
            [((DeviceHeadCell *)cell) isLookReasonWithBool:([self.refreshMgr.refreshDeviceStatus.status isEqualToString:@"设备正常"] ? NO : YES) withBlock:^{
                BaseWebViewController * webVC = [[[BaseWebViewController alloc]initWithURL:@"http://r.ichezheng.com/h5/others/qa/qa0420.html"]navSetupBackItemWithIdentification:BACK_ICON];
                
                webVC.title = @"异常原因";
                [wself.navigationController pushViewController:webVC animated:YES];
            }];
        }
            break;
        case 1:
        {
            NSString *identifier = @"CellDeviceTime";
            cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                              reuseIdentifier:identifier];
                cell.backgroundColor = [UIColor clearColor];
                CGFloat labelY = 24 * __MAIN_RATIO_375;
                [Factory labelWithFrame:CGRectMake(0, labelY, __MAIN_WIDTH, 20) font:FONT_DEFAULT_Light(16) text:@"最后连接时间" textColor:[textLabelColor colorWithAlphaComponent:0.6] onView:cell textAlignment:NSTextAlignmentCenter];
                UILabel * centerLabel = [Factory labelWithFrame:CGRectMake(labelX, labelY + 20 + 5 * __MAIN_RATIO_375, __MAIN_WIDTH - labelX * 2, 25) font:FONT_DEFAULT_Bold(23) text:@"" textColor:textLabelColor onView:cell textAlignment:NSTextAlignmentCenter];
                centerLabel.tag = Cell_Views_Type_CenterLabel;
            }
            cell.backgroundColor = ([self.deviceStatus isEqualToString:@"设备正常"] ? COLOR_HEXSTRING(@"#1f90e5") :  ([self.deviceStatus isEqualToString:@"设备异常"] ?COLOR_HEXSTRING(@"#ef5f33") : [UIColor whiteColor]));
            UILabel * centerLabel = (UILabel *)[cell viewWithTag:Cell_Views_Type_CenterLabel];
            centerLabel.text = self.refreshMgr.refreshDeviceStatus.lastUseDate;
        }
            break;

        case 3:
        {
            NSString *identifier = @"Cell";
            cell = [tableView dequeueReusableCellWithIdentifier:identifier];
            if (cell == nil) {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                              reuseIdentifier:identifier];
                cell.backgroundColor = [UIColor whiteColor];
                
                UILabel * leftLabel = [Factory labelWithFrame:CGRectMake(labelX, 0, 80, CELL_HEIGHT) font:FONT_DEFAULT_Light(16) text:@"" textColor: [Theme wordGrayColor] onView:cell textAlignment:NSTextAlignmentLeft];
                leftLabel.tag = Cell_Views_Type_LeftLabel;
                leftLabel.backgroundColor = [UIColor clearColor];
                
                UILabel *label = [Factory labelWithFrame:CGRectMake(leftLabel.left + leftLabel.width, 0, __MAIN_WIDTH - leftLabel.left * 2 - leftLabel.width, CELL_HEIGHT) font:FONT_DEFAULT_Light(16 ) text:@"" textColor:[Theme wordGrayColor] onView:cell textAlignment:NSTextAlignmentRight];
                label.tag = Cell_Views_Type_RightLabel;
                
            }
            UILabel * leftLabel = (UILabel *)[cell viewWithTag:Cell_Views_Type_LeftLabel];
            leftLabel.text = self.dataArr[indexPath.section][indexPath.row];
            
            
            NSString *str = @"";
            switch (indexPath.row) {
                case 0:
                    str = [STRING_INT(USERINFO.userID) isNotNil] ? STRING_INT(USERINFO.userID): LOCSTRING(@"binding_unknow");
                    break;
                case 1:
                    str = [USERINFO.pastDevice isNotNil] ? USERINFO.pastDevice : LOCSTRING(@"binding_unknow");
                    break;
                case 2:
                    str = [USERINFO.deviceBindingTime isNotNil] ? USERINFO.deviceBindingTime : LOCSTRING(@"binding_unknow");
                    break;
                case 3:
                    str = USERINFO.tel;
                    break;
                    
                default:
                    break;
            }
            UILabel *label = (UILabel *)[cell viewWithTag:Cell_Views_Type_RightLabel];
            label.text = str;
        }
            break;

        case 4:
        case 2:
        {
            NSString * cellID = @"noCellID";
            cell = [tableView dequeueReusableCellWithIdentifier:cellID];
            if (!cell) {
                cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
                cell.backgroundColor = [UIColor whiteColor];
            }
            break;
            
        }
        default:
            break;
    }
    
    

	[cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
}

#pragma mark - nav
-(UIColor *)navBackgroundColor{
    return [UIColor clearColor];
}

@end